package com.whfc.base.service.impl;

import com.whfc.base.dao.AppSceneDetailMapper;
import com.whfc.base.dao.AppSceneInfoMapper;
import com.whfc.base.dto.SceneDTO;
import com.whfc.base.dto.SceneDetailDTO;
import com.whfc.base.entity.AppSceneInfo;
import com.whfc.base.service.AppSceneService;
import com.whfc.common.exception.BizException;
import com.whfc.common.result.ResultEnum;
import org.apache.dubbo.config.annotation.DubboService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @Description 现场模拟
 * @Author hw
 * @Date 2021/3/25 11:36
 * @Version 1.0
 */
@DubboService(interfaceClass = AppSceneService.class, version = "1.0.0")
public class AppSceneServiceImpl implements AppSceneService {

    private Logger logger = LoggerFactory.getLogger(this.getClass());

    @Autowired
    private AppSceneInfoMapper sceneInfoMapper;

    @Autowired
    private AppSceneDetailMapper sceneDetailMapper;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void add(SceneDTO param) throws BizException {
        logger.info("添加现场模拟,param:{}", param);
        List<SceneDetailDTO> details = param.getDetails();
        SceneDTO infoDto = sceneInfoMapper.selectDeptId(param.getDeptId());
        AppSceneInfo info = new AppSceneInfo();
        BeanUtils.copyProperties(param, info);
        if (infoDto == null) {
            sceneInfoMapper.insertSelective(info);

        } else {
            info.setId(infoDto.getSceneId());
            sceneInfoMapper.updateByPrimaryKeySelective(info);
        }
        sceneDetailMapper.deleteBySceneId(info.getId());
        if (details != null && !details.isEmpty()) {
            sceneDetailMapper.insertAll(info.getId(), details);
        }
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void edit(SceneDTO param) throws BizException {
        logger.info("修改现场模拟,param:{}", param);
        List<SceneDetailDTO> details = param.getDetails();
        SceneDTO infoDto = sceneInfoMapper.selectDeptId(param.getDeptId());
        AppSceneInfo info = new AppSceneInfo();
        BeanUtils.copyProperties(param, info);
        if (infoDto == null) {
            sceneInfoMapper.insertSelective(info);
        } else {
            info.setId(infoDto.getSceneId());
            sceneInfoMapper.updateByPrimaryKeySelective(info);
        }
        sceneDetailMapper.deleteBySceneId(info.getId());
        if (details != null && !details.isEmpty()) {
            sceneDetailMapper.insertAll(info.getId(), details);
        }
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void del(Integer id) throws BizException {
        logger.info("删除现场模拟，id:{}", id);
        AppSceneInfo info = sceneInfoMapper.selectByPrimaryKey(id);
        if (info == null) {
            throw new BizException(ResultEnum.FAILURE.getCode(), "需要删除的现场模拟不存在");
        }
        sceneInfoMapper.delById(id);
        sceneDetailMapper.delBySceneId(id);
    }

    @Override
    public SceneDTO details(Integer deptId) throws BizException {
        logger.info("查询现场模拟，deptId：{}", deptId);
        SceneDTO infoDto = sceneInfoMapper.selectDeptId(deptId);
        if (infoDto != null) {
            Integer sceneId = infoDto.getSceneId();
            List<SceneDetailDTO> details = sceneDetailMapper.selectBySceneId(sceneId);
            infoDto.setDetails(details);
        }
        return infoDto;
    }
}
